package com.boarsoft.boar.app.biz;

import java.util.List;

import com.boarsoft.boar.app.AppBiz;
import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.boar.entity.FileVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.app.AppFileBiz;
import com.boarsoft.boar.entity.AppFile;
import com.boarsoft.boar.entity.FileInfo;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component
public class AppFileBizImpl extends SimpleBizImpl implements AppFileBiz {

	@Autowired
	private AppBiz appBiz;

	@Override
	@Transactional(readOnly = true)
	public PagedResult<AppFile> list(String appId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder().append(" where af.appId='")//
				.append(appId).append("'");
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (f.code like '%").append(key);
			sb.append("%' or f.name like '%").append(key);
			sb.append("%' or f.memo like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(new StringBuilder("select count(1) from ")//
				.append(AppFile.class.getName()).append(" af join af.file f")//
				.append(sb).toString());
		if (Util.strIsNotEmpty(orderBy)) {
			String ob = Util.array2str(("f." + orderBy).replaceAll(",", ",").split(","), ",");
			sb.append(" order by ").append(ob);
		} else {
			sb.append(" order by af.no");
		}
		List<AppFile> lt = dao.list(new StringBuilder("select af, f from ")//
				.append(AppFile.class.getName()).append(" af join fetch af.file f")//
				.append(sb).toString(), pageNo, pageSize);
		AppInfo appInfo=appBiz.get(appId);
		for(AppFile f:lt){
			f.setAppCode(appInfo.getCode());
		}
		return new PagedResult<>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public List<AppFile> list(String appId) {
		StringBuilder sb = new StringBuilder()//
				.append(" from ").append(AppFile.class.getName())//
				.append(" af join fetch af.file f where af.appId='")//
				.append(appId).append("' order by af.no");
		return dao.list(sb.toString(), AppFile.class);
	}
	@Transactional(readOnly = true)
	public List<AppFile> listByAppCode(String appCode) {
		StringBuilder sb = new StringBuilder()//
				.append(" from ").append(AppFile.class.getName())//
				.append(" where cAppId = (select cId from app_info where vCode = '")//
				.append(appCode)//
				.append("')");
		return dao.list(sb.toString(), AppFile.class);
	}
	@Override
	@Transactional(readOnly = true)
	public AppFile getByFileName(String appCode, String fileName, String env) {
		StringBuilder sb = new StringBuilder()//
				.append("select af from ").append(AppFile.class.getName())//
				.append(" af join ").append(FileInfo.class.getName())//
				.append(" f on af.fileId = f.id join ").append(FileVersion.class.getName())//
				.append(" fv on fv.fileId = af.fileId join ").append(AppInfo.class.getName())//
				.append(" a on af.appId = a.id where f.code = '")//
				.append(fileName).append("' and fv.status = 1 and fv.env = '").append(env)//
				.append("' and a.code = '").append(appCode).append("'");
		log.info("listByFileName---sql:{}",sb);
		return dao.find(sb.toString(),AppFile.class);
	}
	@Override
	@Transactional(readOnly = true)
	public List<AppFile> getByGroup(String appCode, String group, String env) {
		StringBuilder sb = new StringBuilder()//
				.append("select af from ").append(AppFile.class.getName())//
				.append(" af join ").append(FileVersion.class.getName()).append(" fv  on af.fileId = fv.fileId join ")
				.append(AppInfo.class.getName()).append(" a on af.appId= a.id where fv.status= 1 and fv.env = '")
				.append(env).append("' and fv.group = '")
				.append(group)
				.append("' and a.code = '")
				.append(appCode).append("'");
		log.info("listByGroup---sql:{}",sb);
		return dao.list(sb.toString(), AppFile.class);
	}

	@Override
	@Transactional(readOnly = true)
	public List<AppFile> list(String appId, String fileId) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(appId)) {
			sb.append(" and appId='").append(appId).append("'");
		}
		if (Util.strIsNotEmpty(fileId)) {
			sb.append(" and fileId='").append(fileId).append("'");
		}
		return dao.list(AppFile.class, sb.toString());
	}

	@Override
	@Transactional
	public void save(AppFile o) {
		if(count(o.getAppId(),o.getFileId())>0){
			return;
		}
		dao.save(o);
	}

	@Override
	@Transactional
	public void add(String appId, String fileIds) {
		String fa[] = fileIds.split(",");
		for (String fid : fa) {
			AppFile af = new AppFile();
			af.setAppId(appId);
			af.setFileId(fid);
			this.save(af);
		}
	}

	@Override
	@Transactional
	public AppFile delete(String id) {
		AppFile o = dao.get(AppFile.class, id);
		dao.delete(o);
		return o;
	}

	@Override
	@Transactional(readOnly = true)
	public AppFile get(String id) {
		return (AppFile) dao.get(AppFile.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public List<AppFile> find(String appId, String fileId, Short fileType) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(appId)) {
			sb.append(" and af.appId='").append(appId).append("'");
		}
		if (Util.strIsNotEmpty(fileId)) {
			sb.append(" and af.fileId='").append(fileId).append("'");
		}
		/*if (fileType != null) {
			sb.append(" and af.fileType=").append(fileType);
		}*/

		String ql = new StringBuilder().append("select af from ").append(AppFile.class.getName()).append(" af ")
				.append(" where 1=1 ").append(sb).toString();
		return dao.list(ql, AppFile.class);
	}

	@Override
	@Transactional
	public boolean copy(String oldFileId, String newFileId, String newAppId) {
		// List<AppFile> appFiles = this.find(null, oldFileId, null);
		// if (appFiles == null || appFiles.size() == 0) {
		// return false;
		// }
		// for (AppFile a : appFiles) {
		// AppFile b = new AppFile();
		// b.setAppId(newAppId);
		// b.setFileId(newFileId);
		// this.save(b);
		// }
		return true;
	}

	@Override
	@Transactional(readOnly = true)
	public FileInfo find(String appId, String code) {
		String sql = new StringBuilder().append("select f from ")//
				.append(AppFile.class.getName())//
				.append(" af join af.file f where af.appId='").append(appId)//
				.append("' and f.code='").append(code).append("'").toString();
		return (FileInfo) dao.find(sql);
	}

	@Override
	@Transactional(readOnly = true)
	public int count(String appId, String fileId) {
		String sb = new StringBuilder().append(" from ")//
				.append(AppFile.class.getName())//
				.append(" af where af.appId='").append(appId)//
				.append("' and af.fileId='").append(fileId).append("'").toString();
		return dao.getTotal("select count(af.id)".concat(sb));
	}

}